package com.it.txet;

import com.it.domain.Book;

import java.util.ArrayList;
import java.util.Scanner;

public class bookManager {
    public static void main(String[] args) {
        //图书管理系统
        Scanner sc = new Scanner(System.in);
        ArrayList<Book> list = new ArrayList<>();
        emnueBook(sc, list);


    }
    /**
     * 菜单
     */
    private static void emnueBook(Scanner sc, ArrayList<Book> list) {
        //菜单
        while (true) {
            System.out.println("--------图书管理系统--------");
            System.out.println("1.添加图书");
            System.out.println("2.删除图书");
            System.out.println("3.更改图书");
            System.out.println("4.全部图书");
            System.out.println("5.退出图书系统");
            System.out.println("请输入要操作的功能:");

            int choice = sc.nextInt();
            switch (choice){
                case 1:
                    addBook(list);
                    break;
                case 2:
                    removeBook(list);
                    break;
                case 3:
                    updataBook(list);
                    break;
                case 4:
                    allBookInfo(list);
                    break;
                case 5:
                    System.out.println("退出成功！");
                    System.exit(0);
                default:
                    System.out.println("输入错误！请重新输入:");
                    break;
            }
        }
    }

    /**
     * 添加图书
     */
    private static void addBook(ArrayList<Book> list) {
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入要添加的图书编号：");
        while (true) {
            String id = sc.next();
            int index = searchBook(list,id);
            if(index!=-1){
                System.out.println("该编号已存在！请重新输入:");
            }else {
                System.out.println("请输入图书名:");
                String nameBook = sc.next();
                System.out.println("请输入图书作者名:");
                String authorBook = sc.next();
                System.out.println("请输入图书出版日期:");
                String publicBook = sc.next();
                Book book = new Book(id,nameBook,authorBook,publicBook);
                list.add(book);
                System.out.println("添加成功！");
                break;
            }
        }

    }

    /**
     * 更改图书信息
     */
    private static void updataBook(ArrayList<Book> list) {
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入要更改信息的图书编号：");

        while (true) {
            String id = sc.next();
            int index = searchBook(list, id);
            if (index==-1){
                System.out.println("该图书编号不存在！请重新输入:");
            }else {
                System.out.println("请输入图书名:");
                String nameBook = sc.next();
                System.out.println("请输入图书作者名:");
                String authorBook = sc.next();
                System.out.println("请输入图书出版日期:");
                String publicBook = sc.next();
                Book book = new Book(id,nameBook,authorBook,publicBook);
                list.set(index,book);
                System.out.println("更改成功！");
                break;
            }
        }
    }

    /**
     * 删除图书信息
     */
    private static void removeBook(ArrayList<Book> list) {
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入要删除的图书编号：");

        String id = sc.next();
        int index = searchBook(list, id);
        if (index==-1){
            System.out.println("该图书编号不存在！");
        }else {
            list.remove(index);
            System.out.println("删除成功！");
        }



    }

    /**
     * 查找列表中是否含有该编号图书
     */
    private static int searchBook(ArrayList<Book> list,String id){


        for (int i = 0; i < list.size(); i++) {
            Book book = list.get(i);
            if (book.getBookId().equals(id)){
                return i;
            }
        }
        return -1;
    }


    /**
     * 展示所有图书信息
     */
    private static void allBookInfo(ArrayList<Book> list) {
        if (list.size() == 0 ){
            System.out.println("暂无信息！可添加！");
        }else {
            System.out.println("书号\t\t\t" + "书名\t" + "作者\t" + "出版日期\t");
            for (int i = 0; i < list.size(); i++) {
                Book book = list.get(i);
                System.out.println(book.getBookId()+"\t\t\t"+book.getBookName()+"\t"+book.getBookAuthor()+"\t"+book.getBookPublic());

            }

        }


    }


}
